dnl libcom: An implementation of the Component Object Model
dnl @(#) $Id: configure.ac 66 2009-01-03 04:31:57Z mo@ilaven.net $

dnl Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Mo McRoberts.
dnl
dnl Redistribution and use in source and binary forms, with or without
dnl modification, are permitted provided that the following conditions
dnl are met:
dnl 1. Redistributions of source code must retain the above copyright
dnl    notice, this list of conditions and the following disclaimer.
dnl 2. Redistributions in binary form must reproduce the above copyright
dnl    notice, this list of conditions and the following disclaimer in the
dnl    documentation and/or other materials provided with the distribution.
dnl 3. The names of the author(s) of this software may not be used to endorse
dnl    or promote products derived from this software without specific prior
dnl    written permission.
dnl
dnl THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
dnl INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
dnl AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
dnl AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
dnl SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
dnl TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
dnl PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
dnl LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
dnl NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
dnl SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

AC_INIT(libcom, 0.0)
AC_CONFIG_SRCDIR([include/COM/COM.h])
AM_INIT_AUTOMAKE([foreign])
AM_MAINTAINER_MODE

CPPFLAGS="$CPPFLAGS -W -Wall -D_REENTRANT"
export CPPFLAGS

CFLAGS="$CFLAGS -g -O0"
export CFLAGS

AC_PROG_CC
AC_PROG_CXX
AC_PROG_LIBTOOL

AC_ARG_ENABLE(xpcom, [AS_HELP_STRING([--enable-xpcom],[Enable the libcom/XPCOM bridge (default=auto)])],[use_xpcom=$enableval],[use_xpcom=auto])
AC_ARG_ENABLE(win32, [AS_HELP_STRING([--enable-win32],[Enable the libcom/Win32 bridge (default=auto)])],[use_win32=$enableval],[use_win32=auto])
AC_ARG_ENABLE(cf, [AS_HELP_STRING([--enable-cf],[Use CoreFoundation’s CFPreferences to store registration data  (default=auto)])],[use_cf=$enableval],[use_cf=auto])
AC_ARG_ENABLE(pthread, [AS_HELP_STRING([--enable-pthread],[Use POSIX threads (default=auto)])],[use_pthread=$enableval],[use_pthread=auto])
AC_ARG_ENABLE(dl, [AS_HELP_STRING([--enable-dl],[Use dlopen() for dynamic loading (default=auto)])],[use_dl=$enableval],[use_dl=auto])

AC_MSG_CHECKING([whether to use POSIX threads])
AC_MSG_RESULT([${use_pthread}])
if test x"${use_pthread}" = x"auto" || test x"${use_pthread}" = x"yes" ; then
	pthread_present=yes
	AC_CHECK_HEADER([pthread.h],[AC_DEFINE_UNQUOTED([HAVE_PTHREAD_H],1,[Define to 1 if you have the <pthread.h> header file.])],pthread_present=no)
	if test x"${pthread_present}" = x"yes" ; then
		AC_CHECK_LIB(pthread,pthread_create)
		AC_CHECK_FUNC(pthread_create,,pthread_present=no)
	fi
	if test x"${pthread_present}" = x"no" && test x"${use_pthread}" = x"yes"; then
		AC_MSG_ERROR([POSIX threads support was requested but it cannot be located])
	fi
	use_pthread=${pthread_present}
fi

AC_MSG_CHECKING([whether to use dlopen()])
AC_MSG_RESULT([${use_dl}])
if test x"${use_dl}" = x"auto" || test x"${use_dl}" = x"yes" ; then
	dl_present=yes
	AC_CHECK_HEADER([dlfcn.h],,dl_present=no)
	if test x"${dl_present}" = x"yes" ; then
		AC_CHECK_LIB(dl,dlopen)
		AC_CHECK_FUNC(dlopen,,dl_present=no)
	fi
	if test x"${dl_present}" = x"yes" && test x"${use_dl}" = x"yes" ; then
		AC_MSG_ERROR([dlopen() support was requested but it cannot be located])
	fi
	use_dl=${dl_present}
fi

AC_MSG_CHECKING([whether to use Win32])
AC_MSG_RESULT([${use_win32}])
if test x"${use_win32}" = x"auto" || test x"${use_win32}" = x"yes" ; then
   win32_present=yes
   case "${host}" in
	*-cygwin|*-mingw32)
		LIBS="${LIBS} -lkernel32 -luser32 -ladvapi32 -lole32"
		;;
	*)
		AC_CHECK_HEADER([objbase.h],,[win32_present=no])
		if test x"${win32_present}" = x"yes" ; then
		    AC_CHECK_LIB([ole32],[CoInitializeEx],,[win32_present=no],[-lkernel32 -luser32 -ladvapi32])
		fi
    esac
    if test x"${win32_present}" = x"no" && test x"${use_win32}" = x"yes" ; then
        AC_MSG_ERROR([Win32 requested but could not be found])
    fi
    use_win32="${win32_present}"
fi

AC_MSG_CHECKING([whether to use XPCOM])
AC_MSG_RESULT([${use_xpcom}])
if test x"${use_xpcom}" = x"auto" || test x"${use_xpcom}" = x"yes" ; then
	AC_PATH_PROG([XULRUNNER_CONFIG], [xulrunner-config],,)
	if ! test x"${XULRUNNER_CONFIG}" = x"" ; then
		AC_MSG_CHECKING([for XPCOM])
		XPCOM_LIBS=`${XULRUNNER_CONFIG} --libs xpcom`
		if test x"${XPCOM_LIBS}" = x"" ; then
			AC_MSG_RESULT([not available])
			if test x"${use_xpcom}" = x"yes" ; then
				AC_MSG_ERROR([--enable-xpcom was specified but XPCOM cannot be found])
			fi
			use_xpcom=no
			XPCOM_CPPFLAGS=''
		else
			use_xpcom=yes
			XPCOM_CPPFLAGS=`${XULRUNNER_CONFIG} --cflags xpcom`
			AC_MSG_RESULT([yes, libraries: ${XPCOM_LIBS}; headers: ${XPCOM_CPPFLAGS}])
		fi
	elif test x"${use_xpcom}" = x"yes" ; then
		AC_MSG_ERROR([xulrunner-config is required to configure the XPCOM bridge but it cannot be found])
	fi
fi

AC_MSG_CHECKING([whether to use Core Foundation])
AC_MSG_RESULT([${use_cf}])
if test x"${use_cf}" = x"auto" || test x"${use_cf}" = x"yes" ; then
	CF_LIBS="-framework CoreFoundation"
	CF_CPPFLAGS="-framework CoreFoundation"
	orig_ldflags="${LDFLAGS}"
	LDFLAGS="${LDFLAGS} ${CF_LIBS}"
	AC_CHECK_FUNC([CFPreferencesSetValue],[use_cf=yes],[
	if test x"${use_cf}" = x"yes" ; then
		AC_MSG_ERROR([--enable-cf was specified but cannot locate Core Foundation])
	fi
	use_cf="no"
	CF_LIBS=''
	CF_CPPFLAGS=''
])
	LDFLAGS="${orig_ldflags}"
fi

if test x"${use_xpcom}" = x"yes" ; then
	AC_DEFINE_UNQUOTED([COM_USE_XPCOM], 1, [Define to use XPCOM bindings])
else
	use_xpcom=no
fi
if test x"${use_win32}" = x"yes" ; then
	AC_DEFINE_UNQUOTED([COM_USE_WIN32], 1, [Define to use Win32 bindings])
else
	use_win32=no
fi
if test x"${use_cf}" = x"yes" ; then
	AC_DEFINE_UNQUOTED([COM_USE_CF], 1, [Define to use CoreFoundation for registration data])
else
	use_cf=no
fi
if test x"${use_pthread}" = x"yes" ; then
	AC_DEFINE_UNQUOTED([COM_USE_PTHREAD], 1, [Define to use POSIX threads for locking])
else
	use_pthread=no
fi
if test x"${use_dl}" = x"yes" ; then
	AC_DEFINE_UNQUOTED([COM_USE_DL], 1, [Define to use dlopen() for dynamic loading])
else
	use_dl=no
fi

AC_SUBST([XPCOM_LIBS])
AC_SUBST([XPCOM_CPPFLAGS])

AC_SUBST([CF_CPPFLAGS])
AC_SUBST([CF_LIBS])

AC_CHECK_HEADERS(stdlib.h stdint.h unistd.h sys/sockio.h net/if.h netinet/in.h inttypes.h sys/ioctl.h sys/socket.h net/if_dl.h sys/syscalls.h )
AC_CHECK_FUNCS(srandom jrand48)
AC_CHECK_MEMBER(struct sockaddr.sa_len,
AC_DEFINE_UNQUOTED(HAVE_SA_LEN,1,[Define if struct sockaddr contains sa_len]),,
[#include <sys/types.h>
#include <sys/socket.h>])

AC_CONFIG_SUBDIRS([comidl])
COMIDL='${top_builddir}/comidl/comidl'
AC_SUBST(COMIDL)

UUID_LIBS=''
AC_CHECK_LIB(socket, socket, [UUID_LIB=-lsocket])
AC_SUBST(UUID_LIBS)

JL_BUNDLE_FRAMEWORK([com], [COM])
JL_BUNDLE_FRAMEWORK([dcerpc], [DCE-RPC])

AC_CONFIG_HEADER([config.h])

AC_CONFIG_FILES([Makefile
include/Makefile
include/DCE-RPC/Makefile
include/COM/Makefile
libDCE-RPC/Makefile
libDCE-RPC/uuid/Makefile
libDCE-RPC/rpc/Makefile
libCOM/Makefile
libCOM-stubs/Makefile
compreg/Makefile
uuidgen/Makefile
tests/Makefile
])

AC_OUTPUT

AC_MSG_RESULT([
libcom configuration summary:
	XPCOM bridge:            ${use_xpcom}
	Win32 bridge:            ${use_win32}
	Core Foundation support: ${use_cf}
	POSIX threads:           ${use_pthread}
	dlopen() support:        ${use_dl}
])
